//
//  WKSearchListVC.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/22.
//  Copyright © 2018 何文虎. All rights reserved.
//

#import "WKSearchListVC.h"
#import "WKSearchHeaderView.h"
#import "WKSearchTagCell.h"
#import "WKSearchHeaderView.h"

@interface WKSearchListVC ()<UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UISearchBar *searchBar;
@property(nonatomic,strong)UICollectionView *collectionView;
@end

@implementation WKSearchListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViews];
}

- (void)initViews{
    self.view.backgroundColor = [UIColor whiteColor];
    [self hideMyNaviBar];
    [self addSearchBar];
    [self addTagView];
    
}

- (void)addTagView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
   
    layout.minimumLineSpacing = 12;
    layout.minimumInteritemSpacing = 8;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(0, 12, 0, 12);
    layout.sectionHeadersPinToVisibleBounds = NO;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,NavBarHeight+20,ScreenWidth,ScreenHeight-20-NavBarHeight) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[WKSearchTagCell class] forCellWithReuseIdentifier:NSStringFromClass([WKSearchTagCell class])];
    
    [self.collectionView registerClass:[WKSearchHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([WKSearchHeaderView class])];
    
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    
    _collectionView.layer.masksToBounds = YES;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 5;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WKSearchTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([WKSearchTagCell class]) forIndexPath:indexPath];
    NSArray *tiitleArr = @[@"祛黑头",@"祛痘印",@"美白皮肤",@"撒娇大家都来",@"打算看鹿鼎记按实际打两局"];
    NSString *str = tiitleArr[indexPath.row];
    [cell.tagBtn setTitle:str forState:UIControlStateNormal];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    WKSearchHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([WKSearchHeaderView class]) forIndexPath:indexPath];
    headerView.title = indexPath.section == 0 ? @"最近搜索" : @"热门搜索";
    headerView.indexPath = indexPath;
    return headerView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSArray *tiitleArr = @[@"祛黑头",@"祛痘印",@"美白皮肤",@"撒娇大家都来",@"打算看鹿鼎记按实际打两局"];
    NSString *str = tiitleArr[indexPath.row];
    CGFloat width = [WHUtiities sizeWithString:str font:PFSC_MediumFont(12) constraintSize:CGSizeMake(ScreenWidth-20, 28)].width;
    return CGSizeMake(width+20, 28);
    
//    if (indexPath.row%2==0) {
//         return CGSizeMake(69, 28);
//    }
//    return CGSizeMake(150, 28);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(ScreenWidth, 40);
}


#pragma mark ---搜索控件
- (void)addSearchBar{
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(12, StatusBarHeight+10, ScreenWidth-52-12, 36)];
    [self.view addSubview:self.searchBar];
    
    
    _searchBar.placeholder = @"搜索感兴趣的内容...";
    [self.searchBar setBarTintColor:[UIColor yellowColor]];
    self.searchBar.backgroundImage = [[UIImage alloc] init];
    _searchBar.delegate = self;
    
    UITextField *searchField = [self.searchBar valueForKey:@"searchField"];
    if (searchField) {
        [searchField setBackgroundColor:UIColorFromHex(0xF4F4F4)];
        searchField.textColor = UIColorFromHex(0x3C3C3C);
        searchField.font = PFSC_RegularFont(12);
        searchField.layer.cornerRadius = 36/2;
        //        searchField.layer.borderColor = [UIColor colorWithRed:247/255.0 green:75/255.0 blue:31/255.0 alpha:1].CGColor;
        //        searchField.layer.borderWidth = 1;
        [searchField setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
        searchField.layer.masksToBounds = YES;
    }
    
    
    self.searchBar.tintColor = [UIColor colorWithRed:86/255.0 green:179/255.0 blue:11/255.0 alpha:1];
    
    //修正光标颜色
    [searchField setTintColor:UIColorFromHex(0x3C3C3C)];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(ScreenWidth-52, StatusBarHeight+10, 52, 36);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:BACK_COLOR forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = PFSC_MediumFont(15);
    [self.view addSubview:cancelBtn];
    @weakify(self);
    [[cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, NavBarHeight+10, ScreenWidth, 1)];
    line.backgroundColor = LINEBACK_COLOR;
    line.layer.shadowColor = [UIColor blackColor].CGColor;
    line.layer.shadowOpacity = 0.1f;
    line.layer.shadowOffset = CGSizeMake(0,1);
    [self.view addSubview:line];
}


- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"搜索Begin");
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    NSLog(@"搜索End");
    return YES;
}

// 用户单击取消按钮时激发该方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"----searchBarCancelButtonClicked------");
    // 取消搜索状态
    
}

//当搜索文本框内文本改变时激发该方法
- (void)searchBar:(UISearchBar *)searchBar
    textDidChange:(NSString *)searchText
{
    NSLog(@"----textDidChange------");
    // 调用filterBySubstring:方法执行搜索
    [self searchePatientWithStr:searchText];
}

//用户单击虚拟键盘上Search按键时激发该方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"----searchBarSearchButtonClicked------");
    // 调用filterBySubstring:方法执行搜索
    [self searchePatientWithStr:searchBar.text];
    // 放弃作为第一个响应者，关闭键盘
    [searchBar resignFirstResponder];
}



- (void)searchePatientWithStr:(NSString*)str{
    

}


@end
